package com.zjs.demomongo.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.mongodb.DB;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
@Service
public class MongoTools {
	@Autowired
    private MongoTemplate mongoTemplate;
//  参考网址	
//	http://blog.csdn.net/luo_cheng_yibuzhiyao/article/details/53301954
//	http://blog.csdn.net/zhu_tianwei/article/details/44278095
	/**
     * 存储文件 
     * @param collectionName 集合名 
     * @param file 文件 
     * @param fileid 文件id 
     * @param filename 文件名称
     */
    public void saveFile(String collectionName, File file, String fileid,String filename) {
        try {
            DB db = mongoTemplate.getDb();
            // 存储fs的根节点
            GridFS gridFS = new GridFS(db, collectionName);
            GridFSInputFile gfs = gridFS.createFile(file);
            gfs.put("aliases", "nova");
            gfs.put("filename", fileid);
            gfs.put("contentType", filename.substring(filename.lastIndexOf(".")));
            gfs.save();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("存储文件时发生错误！！！");
        }
    }

    /**
     * 提取文件
     * @param collectionName
     * @param filename
     * @return
     */
    @SuppressWarnings("resource")
	public GridFSDBFile readFileOne(String collectionName, String filename) {
        try {
        	 long startTime=System.currentTimeMillis();
             
            DB db = mongoTemplate.getDb();
            // 获取fs的根节点
            GridFS gridFS = new GridFS(db, collectionName);
            GridFSDBFile dbfile = gridFS.findOne(filename);
            if (dbfile != null) {
            	System.out.println("------开始------");
            	InputStream is = dbfile.getInputStream();
            	XSSFWorkbook book = new XSSFWorkbook(is);
            	XSSFSheet sheet = book.getSheetAt(0);
            	int rows = book.getSheetAt(0).getLastRowNum();

            	//解析Excel时区分类型 xlsx 用XSSFWorkbook XSSFSheet XSSFRow   2003版  xls 用HSSFWorkbook HSSFSheet HSSFRow
            	for(int k =0;k<rows;k++) {
            		XSSFRow row = sheet.getRow(k);
            		String rowtext = "";
            		for (int columns = 0; columns < 43; columns++) {
            			//System.out.println(columns);
                		XSSFCell cell = row.getCell(columns);
                		if(null!=cell) {
                			String celltext = cell.getStringCellValue();
                    		if(null!=celltext) {
                    			rowtext += celltext +"--"+columns+"--";
                    		}else {
                    			rowtext +=" == " +"--"+columns+"--";
                    		}
                		}else {
                			rowtext +=" == " +"--"+columns+"--";
                		}	
                	}
            		System.out.println(rowtext);
            	}
            	 long endTime=System.currentTimeMillis();
                 float excTime=(float)(endTime-startTime)/1000;
                 System.out.println("执行时间："+excTime+"s");

                 System.out.println("------结束------解析行数："+ rows);
             	
                is.close();  
            	
            	
                return dbfile;
            }
        } catch (Exception e) {
            // TODO: handle exception
        	e.printStackTrace();
        	System.out.println("读取文件时发生错误！！！");
        }
        return null;
    }
    
    /**
     * 删除文件 
     * @param collectionName 集合名 
     * @param fileid 文件id 
     */
    public void removeFile(String collectionName,String fileid) {
        try {
            DB db = mongoTemplate.getDb();
            // 存储fs的根节点
            GridFS gridFS = new GridFS(db, collectionName);
            gridFS.remove(fileid);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("删除文件时发生错误！！！");
        }
    }
	
}